package edu.sjsu.cs.queryopt.dbms.main;

import java.util.ArrayList;
import java.util.Collections;

 
public class FilterFactor {
	static CalCard cc = new CalCard();
	static AccessTypeOne ato = new AccessTypeOne();
	static ArrayList<String> fftran = new ArrayList<String>();
	public void FindSeq(){
		
		/**
		 * CASE 1: R SCAN CASE WITH PREDICATE
		 * uf 2 OR MORE , GET THE ORER OF PREDUCATE PROCESSING
		 * BASED ON FF LOWEST SHOULD BE FIRST
		 * */
		

			if(cc.prediTable.size() >1){
				LowFFFirst();
			}
			else{
				
				cc.prediTable.get(0).set(5, "1");
			}
			
			
		
		
		
	}
	public void LowFFFirst(){
		ArrayList<String> FFval = new ArrayList<String>();
		for(int index = 0 ; index < cc.prediTable.size() ; index++){
			
			FFval.add(cc.prediTable.get(index).get(3));
			
		}
		Collections.sort(FFval, String.CASE_INSENSITIVE_ORDER);
//		System.out.println("FFval " + FFval);
		for(int i = 0 ; i < FFval.size() ; i++){
			for(int ii = 0 ;  ii <  cc.prediTable.size(); ii++){
				
				if( cc.prediTable.get(ii).contains(FFval.get(i))){
					cc.prediTable.get(ii).set(5, Integer.toString(i+1));
					
				}
				
			}
		}
	}
	/**
	 * tran checker ver 1. only handle one local and one join case
	 * i wish i have more time then i can handle everything
	 * */
	public boolean isTran(ArrayList<String>conditionpod ){
		String startTran = "";
		String enfTran = "";
		boolean gateofTruth = false;
		boolean iloveKatherine = false;
		if(ato.IsJoinPredicate(conditionpod)){
			
			for(int index = 0 ; index < conditionpod.size(); index++){
				
				if(conditionpod.get(index).trim().equals("=")){
					if(conditionpod.get(index-1).startsWith("T") && conditionpod.get(index+1).startsWith("T") ){
						startTran = conditionpod.get(index-1);
						enfTran = conditionpod.get(index+1);
						gateofTruth = true;
					}
					if(conditionpod.get(index-1).startsWith("T") && !conditionpod.get(index+1).startsWith("T")){
						/**
						 * asumme we have we have T1.2= T2.1 
						 * and enfTran = T2.2
						 * then checking local prediate begin is same
						 * */
						
						if(enfTran.equals(conditionpod.get(index-1))){
							iloveKatherine = true;
						}
					}
					
				}
				
			}
			/**
			 * if we have T1.2= T2.1 AND T2.1 = 100 then we assume that we have tran
			 * */
			if(gateofTruth== true && iloveKatherine==true){
				/**
				 * if gateofTruth is true and iloveKatherine is true 
				 * then there are trans
				 * */
				return true;
			}
		}
		else{/**
		if there are no join then it can not be tran, 
		we need to at least one join
		*/
			return false;
		}
		return iloveKatherine;
	}
	/**
	 * assume there is tran so let make tran form yay
	 * */
	public void makeTran(ArrayList<String>conditionpod)
	{
		String startTran = "";
		String enfTran = "";
		String tranass = "";
		String transassvalue = "";
		for(int index = 0 ; index < conditionpod.size(); index++){
			
			if(conditionpod.get(index).trim().equals("=")){
				if(conditionpod.get(index-1).startsWith("T") && conditionpod.get(index+1).startsWith("T") ){
					startTran = conditionpod.get(index-1);
					enfTran = conditionpod.get(index+1);
					
				}
				if(conditionpod.get(index-1).startsWith("T") && !conditionpod.get(index+1).startsWith("T")){
					/**
					 * asumme we have we have T1.2= T2.1 
					 * and enfTran = T2.2
					 * then checking local prediate begin is same
					 * */
					tranass = conditionpod.get(index-1);
					
				
				}
				
			}
			
		}
		
		for(int ii = 0 ; ii < conditionpod.size();ii++){
			/**
			 * real basic case, again .. if i have one more day 
			 * i can handle more than this...
			 * ah.... hate prof!!!! 
			 * i wish he offer the for infor before then thanks giving!!!
			 * 
			 * anyway...
			 * based on the "="
			 * first, check local predicate
			 * second, check the left hand is matching with tranass variable
			 * if same -> tran build form
			 * if not same -> keep looping until u kiss transs's ass
			 * checkthe left hand side
			 * 
			 * */
			if(conditionpod.get(ii).trim().equals("=")){
				
				if(conditionpod.get(ii-1).startsWith("T") && !conditionpod.get(ii+1).startsWith("T")){
					if(tranass.trim().equals(conditionpod.get(ii-1).trim())){
						transassvalue = conditionpod.get(ii+1);
					}
				}
				
			}
			
		}
		fftran.add("E");
		fftran.add(" ");//cc1
		fftran.add(" ");//cc2
		fftran.add(" ");//ff1
		fftran.add(" ");//ff2
		fftran.add("N/A");
		fftran.add(startTran + "=" + transassvalue);
		fftran.add("TCP");
		
		
	}

}
